package com.joplus.housekeeper.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ExpandableListView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.adapter.ImSearchAdapter;
import com.joplus.housekeeper.base.BaseActivity;
import com.joplus.housekeeper.bean.ImSearchAccountBean;
import com.joplus.housekeeper.bean.ImSearchBean;
import com.joplus.housekeeper.bean.ImSearchMessageBean;
import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;
import com.joplus.housekeeper.ui.view.LoadMoreexListView;
import com.joplus.housekeeper.utils.KeyBoardUtils;
import com.joplus.housekeeper.utils.T;

/**
 * Created by zisheng on 2018/7/4.
 */

public class ImSearchActivity extends BaseActivity {

    private LoadMoreexListView lv;
    private EditText et;
    private ImSearchAdapter imSearchAdapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_search_im;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        et = (EditText) findViewById(R.id.et);
        lv = (LoadMoreexListView) findViewById(R.id.lv);
    }

    @Override
    protected void initDatas() {

    }

    int startRowA = 0;
    int startRowM = 0;

    @Override
    protected void initEvents() {
        lv.setOnLoadMoreListener(new LoadMoreexListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {

            }
        });

        et.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(final View v, int keyCode, KeyEvent event) {
                if (et.getText() == null || et.getText().toString().trim().length() <= 0) {
                    return false;
                }

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    startRowA = 0;
                    startRowM = 0;
                    getData();
                    imSearchAdapter = new ImSearchAdapter();
                    lv.setAdapter(imSearchAdapter);
                    lv.setGroupIndicator(null);
                    for (int i = 0; i < imSearchAdapter.getGroupCount(); i++) {
                        lv.expandGroup(i);
                    }
                    return true;
                }

                return false;
            }
        });
    }

    private void getData() {
        RequestHelper.getInstance().searchAccount(context, startRowA, et.getText().toString(), new TextCallback() {
            @Override
            public void onFailure(String responseString) {

            }

            @Override
            public void onSuccess(Object responseString) {
                ImSearchAccountBean responseString1 = (ImSearchAccountBean) responseString;
                if (!responseString1.getCode().equals("2000")) {
                    T.showShort(context, responseString1.getMessage());
                    return;
                }
                if (responseString1.getData() != null && responseString1.getData().getMessageAccountList() != null) {

                    imSearchAdapter.setAccountRecord(responseString1.getData().getMessageAccountList());
                    imSearchAdapter.notifyDataSetChanged();  startRowA = responseString1.getData().getStartRow();
                }
            }
        });
        RequestHelper.getInstance().searchMessage(context, startRowM, et.getText().toString(), new TextCallback() {
            @Override
            public void onFailure(String responseString) {

            }

            @Override
            public void onSuccess(Object responseString) {
                ImSearchMessageBean responseString1 = (ImSearchMessageBean) responseString;
                if (!responseString1.getCode().equals("2000")) {
                    T.showShort(context, responseString1.getMessage());
                    return;
                }
                if (responseString1.getData() != null && responseString1.getData().getMessageRecordWithBLOBsList() != null) {

                    imSearchAdapter.setMessageRecord(responseString1.getData().getMessageRecordWithBLOBsList());
                    imSearchAdapter.notifyDataSetChanged();
                    startRowA = responseString1.getData().getStartRow();
                }
            }
        });
    }

    @Override
    protected boolean isSlideFinishActivity() {
        return false;
    }

    @Override
    protected int setTranslucentStatusColor() {
        return 0;
    }
}
